//
//  ViewController.m
//  myhuibao
//
//  Created by lijun on 13-2-4.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//
#import "HomeViewController.h"
#import "HouseViewController.h"
#import "RestaurantViewController.h"
#import "NearSearchViewController.h"
@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self startUpdates];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"主页";
        
    }
    return self;
}

- (IBAction)showHouse:(id)sender {
    HouseViewController *view = [[HouseViewController alloc] initWithNibName:@"HouseViewController" bundle:nil] ;
    view.showback = 1;
    [[ self navigationController] pushViewController:view animated:YES];
    [view release];
}

- (IBAction)showRestaurant:(id)sender {
    RestaurantViewController *view = [[[RestaurantViewController alloc] initWithNibName:@"RestaurantViewController" bundle:nil] autorelease] ;
    view.showback = 1;
    [[ self navigationController] pushViewController:view animated:YES];
//    [view release];
}

- (IBAction)showNear:(id)sender {
    NearSearchViewController *view = [[NearSearchViewController alloc] initWithNibName:@"NearSearchViewController" bundle:nil] ;
    [[ self navigationController] pushViewController:view animated:YES];
    [view release];
}


- (void)startUpdates
{
    // Create the location manager if this object does not
    // already have one.
//    if (nil == locationManager)
//        locationManager = [[CLLocationManager alloc] init];
//    
//    locationManager.delegate = self;
//    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
//    
//    // Set a movement threshold for new events
//    locationManager.distanceFilter = 500;
//    
//    [locationManager startUpdatingLocation];
    locationManager = [[CLLocationManager alloc] init];//创建位置管理器
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=200.0f;
    //启动位置更新
    [locationManager startUpdatingLocation];
    
}


// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    // If it's a relatively recent event, turn off updates to save power
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 5.0)
    {
        [manager stopUpdatingLocation];
        
        printf("latitude %+.6f, longitude %+.6f\n",
               oldLocation.coordinate.latitude,
               oldLocation.coordinate.longitude);
        MKReverseGeocoder *reverseGeocoder =[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
        NSLog(@"%g",newLocation.coordinate.latitude);
        NSLog(@"%g",newLocation.coordinate.longitude);
        NSLog(@"%g",newLocation.horizontalAccuracy);

        reverseGeocoder.delegate = self;
        [reverseGeocoder start];
        
    }
    // else skip the event and process the next one.
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    NSLog(@"MKReverseGeocoder has failed.");
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    
    NSLog(@"当前地理信息为：%@",placemark);
}

@end
